package com.weakteam.orderonline.dataresource.domain;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * @author Nail
 */
@Entity
@javax.persistence.Table(name = "\"CLIENT_ORDER\"")
public class ClientOrder implements Serializable {

    private static final Logger log = LoggerFactory.getLogger(ClientOrder.class);

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "\"CLIENT_ORDER_id_seq\"")
    @SequenceGenerator(name = "\"CLIENT_ORDER_id_seq\"", sequenceName = "\"CLIENT_ORDER_id_seq\"")
    @Column(name = "\"id\"", nullable = false)
    private Integer id;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "\"created\"")
    private Date created;

    @Column(name = "\"status\"", nullable = false)
    private String status;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "\"lastAction\"")
    private Date lastAction;

    @ManyToOne(optional = false)
    @JoinColumn(name = "\"placeId\"", nullable = false)
    private Place place;

    @OneToMany(mappedBy="clientOrder",cascade=CascadeType.ALL,fetch=FetchType.EAGER)
    private List<ClientOrderContent> content;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Date getLastAction() {
        return lastAction;
    }

    public void setLastAction(Date lastAction) {
        this.lastAction = lastAction;
    }

    public Place getPlace() {
        return place;
    }

    public void setPlace(Place place) {
        this.place = place;
    }

    public List<ClientOrderContent> getContent() {
        return content;
    }

    public void setContent(List<ClientOrderContent> content) {
        this.content = content;
    }

    public String getJSON() {
        StringBuilder json = new StringBuilder();
        json.append("{\"id\":").append(id).append(",");
        json.append("\"created\":\"").append(created).append("\",");
        json.append("\"status\":\"").append(status).append("\",");
        json.append("\"lastAction\":\"").append(lastAction).append("\",");
        json.append("\"placeName\":\"").append(place.getName()).append("\",");
        json.append("\"orderContent\":");
        String comma ="";
        for (ClientOrderContent iContent : content) {
            json.append(comma).append(iContent.getJSON());
            comma = ",";
        }
        json.append("}");
        return json.toString();
    }

}
